package com.xmbandu.app.base;

import android.app.Application;
import android.content.Context;
import android.support.multidex.MultiDex;
import android.util.Log;

import com.everobo.robot.sdk.ReadBookInterface;
import com.tencent.bugly.crashreport.CrashReport;
import com.xmbandu.app.utils.AppUtils;
import com.everobo.robot.sdk.ReadBookOption;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

import cn.jpush.android.api.JPushInterface;


//import android.support.multidex.MultiDex;

/**
 * @author wmq
 * @name 小猫慧读
 * @package com.xmbandu.app
 * @class describe
 * @create 2019/4/18 18:04
 */


public class MyApp extends Application {
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        //MultiDex.install(base);
        MultiDex.install(this);
//        JPushInterface.setDebugMode(true);
//        JPushInterface.init(this);
    }


    static public void copyFilesFassets(Context context, String oldPath, String newPath, Runnable runner) {
        try {
            boolean  isRunUndone = true;
            String fileNames[] = context.getAssets().list(oldPath);//获取assets目录下的所有文件及目录名
            if (fileNames.length > 0) {//如果是目录
                File file = new File(newPath);
                file.mkdirs();//如果文件夹不存在，则递归
                for (String fileName : fileNames) {
                    copyFilesFassets(context,oldPath + "/" + fileName,newPath+"/"+fileName,runner);
                }
            } else {//如果是文件
                InputStream is = context.getAssets().open(oldPath);
                FileOutputStream fos = new FileOutputStream(new File(newPath));
                byte[] buffer = new byte[1024];
                int byteCount=0;
                while((byteCount=is.read(buffer))!=-1) {//循环从输入流读取 buffer字节
                    fos.write(buffer, 0, byteCount);//将读取的输入流写入到输出流
                }
                fos.flush();//刷新缓冲区
                is.close();
                fos.close();
            }
            if(isRunUndone){
                if(runner!=null){
                    runner.run();
                }
                isRunUndone = false;
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            //如果捕捉到错误则通知UI线程
//	                   MainActivity.handler.sendEmptyMessage(COPY_FALSE);
        }
    }
    @Override
    public void onCreate() {
        super.onCreate();
        AppUtils.init(this);
        JPushInterface.setDebugMode(true);
        JPushInterface.init(this);
        CrashReport.initCrashReport(this, "ed82c5a5a4", false);
//        String params= "{\"amat\": \"\", \"distort\": \"\", \"homomat\": \"0.5864824448544375,-0.2758598839165519,150.42900491143524,0.0131475912452495,0.5715770576863622,-98.243629315654,8.393899743353375e-06,-0.0010609285161345416,1.2878064434941254\", \"expandratio\": \"0.0,0.15,-0.35,0.1\", \"size\": \"494, 560\", \"flipflag\": 0, \"coverScore\": 15, \"contentScore\": 15, \"cameraType\": 1}";
//        String params= "{\"amat\": \"\", \"distort\": \"\", \"homomat\": \"0.5874053030555177,-0.38652074522575747,149.15795358734522,-0.007021734946403389,0.6196028587506749,-32.52353336200678,-2.5225763529069068e-05,-0.001377076031009622,1.1770246500020225\", \"expandratio\": \"0.0,0.0,-0.3,0.0\", \"size\": \"545, 560\", \"flipflag\": 0, \"coverScore\": 15, \"contentScore\": 15, \"cameraType\": 0}";
        ReadBookInterface.init(this,"erpwtp10002001","46ee587f07a0ddce9e1dccc296d3a3c2");
        //以下为可选配置
        String params= "{\"amat\": \"\", \"distort\": \"\", \"homomat\": \"0.5864824448544375,-0.2758598839165519,150.42900491143524,0.0131475912452495,0.5715770576863622,-98.243629315654,8.393899743353375e-06,-0.0010609285161345416,1.2878064434941254\", \"expandratio\": \"0.0,0.15,-0.35,0.1\", \"size\": \"494,560\"}";
        ReadBookOption readBookOption = new ReadBookOption();
        readBookOption
                //配置相机识别参数
                .setCameraParams(params)
                //配置前后置相机默认前置1
                .setCameraType(1)
                //配置识别内容页分数值阀
                .setContentScore(10)
                .setShowSimilarDialog(false)
                //配置识别封面页分数值阀
                .setCoverScore(10);
              ReadBookInterface.setReadBookOption(readBookOption);

//        ReadBookInterface.setCameraParams(params);
//        Log.e("sdkVersion", "onCreate: "+ReadBookInterface.getEveroboRpSdkVersion());
        //new com.everobo.robot.phone.ui.MainActivity();
    }
}
